package com.qf.first.base.untils;

import com.qf.first.base.pojo.vo.UserVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

public class ShiroUtils {
    public static final int HASH_ITERATIONS = 1024;


    public static String sha256(String password){
        return new SimpleHash(Sha256Hash.ALGORITHM_NAME,password,null,HASH_ITERATIONS).toString();
    }

    public static void main(String[] args) {
        System.out.println(sha256("admin"));
        //d523b10fb5f77d2af41ecd16cff281461006646cde9777e5ad0b6ec1ec6c3d77
    }


    /**
     * 获取会话
     */
    public static Session getSession() {
        return SecurityUtils.getSubject().getSession();
    }

    /**
     * Subject：主体，代表了当前“用户”
     */
    public static Subject getSubject() {
        return SecurityUtils.getSubject();
    }

    public static UserVo getUser() {
        return (UserVo) SecurityUtils.getSubject().getPrincipal();
    }

    public static Long getUserId() {
        return getUser().getUserId();
    }

    public static void setSessionAttribute(Object key, Object value) {
        getSession().setAttribute(key, value);
    }

    public static Object getSessionAttribute(Object key) {
        return getSession().getAttribute(key);
    }

    public static boolean isLogin() {
        return SecurityUtils.getSubject().getPrincipal() != null;
    }

    public static void logout() {
        SecurityUtils.getSubject().logout();
    }
}
